﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ESAM_Assignment_3
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Retrieve the user's details from the cookie
            CookieClass myCookie = new CookieClass();
            myCookie.ReadCookie(Context);

            lblUsername.Text = myCookie.Username;
            tbxCurrentPassword.Text = myCookie.Password;
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            string username = Request.QueryString["Username"];
            string CurrentPassword = tbxCurrentPassword.Text;
            string NewPassword = tbxNewPassword.Text;

            bool status = UserClass.ChangePassword(username, CurrentPassword, NewPassword);

            if (status)
            {
                lblStatus.Text = "Success. You new password has been saved. Please use the new password at your next logon.";
                lblStatus.ForeColor = System.Drawing.Color.Blue;

                //Store user's details since password has been changed
                CookieClass myCookie = new CookieClass();
                myCookie.WriteCookie(Context, username, NewPassword);
            }
            else
            {
                lblStatus.Text = "Error! Your new password cannot be saved. Incorrect current password? Please try again.";
                lblStatus.ForeColor = System.Drawing.Color.Red;
            }

            //Reset the textboxes
            tbxCurrentPassword.Text = "";
            tbxNewPassword.Text = "";
            tbxRetypePassword.Text = "";
        }
    }
}